#!/usr/bin/env python

import pygame, sys, math, random, time
import Numeric

from accel import AccelControl

from vector import *

MAXRADIUS=35.0

current=None


def get_value(y, i):

	return 20+100*i+48+float(y)*(96.0/2000.0)




def input(events):
	global current
	
	for event in events:
		if event.type == pygame.QUIT: 
			sys.exit(0)
		elif event.type == pygame.KEYDOWN:
			# scancode 177 = aux
			if event.key == pygame.K_ESCAPE:
				sys.exit(0)
		elif event.type == pygame.MOUSEBUTTONDOWN:
			pass
		elif event.type == pygame.MOUSEBUTTONUP:
			pass
	   

def load_sound(name):
	class NoneSound:
		def play(self): pass
	if not pygame.mixer:
		return NoneSound()
	try:
		sound = pygame.mixer.Sound(name)
	except pygame.error, message:
		print "Cannot load sound:", name
		raise SystemExit, message
	return sound


if __name__=="__main__":
	pygame.init()

	if not pygame.mixer: print 'Warning, sound disabled'

	#window = pygame.display.set_mode((480, 640))
	window = pygame.display.set_mode((480, 640), pygame.FULLSCREEN)
	
	screen = pygame.display.get_surface() 

	# load sound

	# creates graphics

	a=AccelControl()

	cx=(0,255,0)
	cy=(255,0,0)
	cz=(255,255,0)
	cwhite=(255,255,255)
	cgrey=(100,100,100)

	if pygame.font:
		font = pygame.font.Font(None, 16)

		text = font.render("X", 1, cx)
		screen.blit(text,(0,0))
		text = font.render("Y", 1, (0,0,255))
		screen.blit(text,(16,0))
		text = font.render("Z", 1, (0,255,255))
		screen.blit(text,(32,0))

		text = font.render("Accel 1 on top.", 1, cgrey)
		screen.blit(text,(48,0))

		
		
	last=time.time()
	print "Show Accelerators!"

	line=0

	x1=y1=z1=0
	x2=y2=z2=0
	
	
	while True: 
		input(pygame.event.get())

		pygame.draw.line(screen,0,(line,20),(line,639))

		screen.set_at((line, get_value(0,0)), cgrey)
		screen.set_at((line, get_value(0,1)), cgrey)
		screen.set_at((line, get_value(0,2)), cgrey)
		screen.set_at((line, get_value(0,3)), cgrey)
		screen.set_at((line, get_value(0,4)), cgrey)
		screen.set_at((line, get_value(0,5)), cgrey)
				


		screen.set_at((line, get_value(x1,0)), cx)
		screen.set_at((line, get_value(y1,1)), cy)
		screen.set_at((line, get_value(z1,2)), cz)

		screen.set_at((line, get_value(x2,3)), cx)
		screen.set_at((line, get_value(y2,4)), cy)
		screen.set_at((line, get_value(z2,5)), cz)

		line+=1
		if line==479: line=0
		
		pygame.draw.line(screen,cwhite,(line,20),(line,639))
		# input: 
 		x1,y1,z1=a.read1()
		x2,y2,z2=a.read2()
				
		pygame.display.flip()

		# dump debug things

		now=time.time()

		
		sys.stdout.write("\r(% 5.2f, % 5.2f, % 5.2f), (% 5.2f, % 5.2f, % 5.2f), FPS: % 2.2f"%(x1,y1,z1,x2,y2,z2,1.0/(now-last)))
		last=now

		sys.stdout.flush()


